import wallsData from '@/assets/map.json'

export default class AiTank {
  constructor(tank) {
    this.tank = tank;
    this.aim = 0; // 坦克的移动方向 0 上 1 右 2 下 3 左
    this.speed = 1;
    this.position = {
      x: 0,
      y: 0,
    }
    this.changeDirectionInterval = 2000; // 每2秒可能改变一次方向
    this.lastDirectionChange = Date.now();
  }

  move() {
    // 随机改变方向
    if (Date.now() - this.lastDirectionChange > this.changeDirectionInterval) {
        if (Math.random() < 0.3) { // 30%的概率改变方向
            this.randomAim();
        }
        this.lastDirectionChange = Date.now();
    }

    // 首先判断是否到达边界
    if (this.aim === 0 && this.position.y - this.speed < 0) {
      // 随机生成方向
      this.randomAim();
      return;
    } else if (this.aim === 1 && this.position.x + this.speed > 750) {
      // 随机生成方向
      this.randomAim();
      return;
    } else if (this.aim === 2 && this.position.y + this.speed > 750) {
      // 随机生成方向
      this.randomAim();
      return;
    } else if (this.aim === 3 && this.position.x - this.speed < 0) {
      // 随机生成方向
      this.randomAim();
      return;
    } else if (this.checkCollision()) {
      // 随机生成方向
      this.randomAim();
      return;
    }
    // 根据方向移动
    switch (this.aim) {
      case 0:
        this.position.y -= this.speed;
        break;
      case 1:
        this.position.x += this.speed;
        break;
      case 2:
        this.position.y += this.speed;
        break;
      case 3:
        this.position.x -= this.speed;
        break;
    }
  }
  // 随机生成方向
  randomAim() {
    const random = Math.random();
    if (random <= 0.25) {
      this.aim = 0;
    } else if (random <= 0.5) {
      this.aim = 1;
    } else if (random <= 0.75) {
      this.aim = 2;
    } else {
      this.aim = 3;
    }
  }
  checkCollision() {
    const tankX = this.position.x;
    const tankY = this.position.y;
    const tankWidth = 50;
    const tankHeight = 50;
    const direction = this.aim;
    const speed = this.speed;

    for (let wall of wallsData) {
      const wallX = wall[0];
      const wallY = wall[1];
      const wallWidth = 50;
      const wallHeight = 50;

      if (direction === 0) {
        // 坦克向上移动时的碰撞检测
        if (
            tankX < wallX + wallWidth &&
            tankX + tankWidth > wallX &&
            tankY < wallY + wallHeight &&
            tankY + speed < wallY + wallHeight &&
            tankY + speed > wallY
        ) {
          return true; // 碰撞发生
        }
      } else if (direction === 2) {
        // 坦克向下移动时的碰撞检测
        if (
            tankX < wallX + wallWidth &&
            tankX + tankWidth > wallX &&
            tankY < wallY + wallHeight &&
            tankY + tankHeight + speed > wallY &&
            tankY + tankHeight < wallY + wallHeight
        ) {
          return true; // 碰撞发生
        }
      } else if (direction === 3) {
        // 坦克向左移动时的碰撞检测
        if (
            tankX - speed < wallX + wallWidth &&
            tankX - speed > wallX &&
            tankY < wallY + wallHeight &&
            tankY + tankHeight > wallY
        ) {
          return true; // 碰撞发生
        }
      } else if (direction === 1) {
        // 坦克向右移动时的碰撞检测
        if (
            tankX + tankWidth + speed > wallX &&
            tankX + tankWidth < wallX + wallWidth &&
            tankY < wallY + wallHeight &&
            tankY + tankHeight > wallY
        ) {
          return true; // 碰撞发生
        }
      }
    }
    return false; // 没有碰撞
  }
}
